﻿using AudioBooks.Data;
using SQLite;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AudioBooks.SQLite
{
    public static class HandlePlayChapter
    {
        public static bool SaveDataPlayChapterInfo(PlayChapterInfo data)
        {
            bool result = false;
            using (var db = new SQLiteConnection(App.DatabasePath))
            {
                try
                {
                    db.Insert(data);
                    result = true;
                }
                catch (Exception ex)
                {
                    result = false;
                }
            }
            return result;
        }
        public static ObservableCollection<PlayChapterInfo> GetDataPlayChapterInfos()
        {
            using (var db = new SQLiteConnection(App.DatabasePath))
            {
                var dataUser = (db.Table<PlayChapterInfo>()).ToList();
                if (dataUser != null && dataUser.Count > 0)
                    return new ObservableCollection<PlayChapterInfo>(dataUser);
                else
                    return null;
            }

        }
        public static ObservableCollection<PlayChapterInfo> GetDataPlayChapterInfosByBookId(string bookId, string chapterName)
        {
            using (var db = new SQLiteConnection(App.DatabasePath))
            {
                var dataUser = (db.Table<PlayChapterInfo>()).ToList();
                if (dataUser != null && dataUser.Count > 0)
                {
                    var data = dataUser.Where(m => m.BookId == bookId.Trim() && m.ChapterName == chapterName.Trim()).ToList();
                    if (data != null && data.Count > 0)
                        return new ObservableCollection<PlayChapterInfo>(data);
                }
                return null;
            }
        }
        public static ObservableCollection<PlayChapterInfo> GetDataPlayChapterInfosByBookId(string bookId )
        {
            using (var db = new SQLiteConnection(App.DatabasePath))
            {
                var dataUser = (db.Table<PlayChapterInfo>()).ToList();
                if (dataUser != null && dataUser.Count > 0)
                {
                    var data = dataUser.Where(m => m.BookId == bookId.Trim()).ToList();
                    if (data != null && data.Count > 0)
                        return new ObservableCollection<PlayChapterInfo>(data);
                }
                return null;
            }
        }
        //public static PlayChapterInfo CheckExitsBook(int bookId)
        //{
        //    using (var db = new SQLiteConnection(App.DatabasePath))
        //    {
        //        var dataUser = (db.Table<PlayChapterInfo>()).FirstOrDefault(m => m.BookId == bookId);
        //        if (dataUser != null)
        //            return dataUser;
        //        else
        //            return null;
        //    }

        //}
    }
}
